
#include "WinMain.h"

WinMain::WinMain()
{
	this->setWindowTitle("~ JTwitt ~");	
	this->setWindowIcon(QIcon(ICONE));
	this->setFixedSize(800, 600);

	_http = new QHttp("www.twitter.com", 80, this);

	_wl = new WinLogin();
	_wl->show();
	this->setCentralWidget(_wl);
	QObject::connect(_wl->getButton(), SIGNAL(clicked()), this, SLOT(changeWindow()));

	//	_wt = new WinTalk();

	/*
	_wl = new WinLogin();
	_wl->show();
	this->setCentralWidget(_wl);
	*/
}

WinMain::~WinMain()
{
}

void WinMain::changeWindow()
{
	// Login:Pwd remplis
	if (!(_wl->getLogin().isEmpty()) && !(_wl->getPassword().isEmpty()))
	{
		// On check si necessite un proxy
		if (!(_wl->getPHost().isEmpty()) && !(_wl->getPPort().isEmpty()))
			if (!(_wl->getPUsername().isEmpty()) && !(_wl->getPPassword().isEmpty()))
				_http->setProxy(_wl->getPHost(), _wl->getPPort().toInt(), _wl->getPUsername(), _wl->getPPassword());
			else
				_http->setProxy(_wl->getPHost(), _wl->getPPort().toInt());

		// On tente la connexion a twitter
		_http->setUser(_wl->getLogin(), _wl->getPassword());
		_http->get("/account/verify_credentials.xml");
		connect(_http, SIGNAL(done(bool)), this, SLOT(httpRequestDone(bool)));
		connect(_http, SIGNAL(requestStarted(int)), this, SLOT(httpRequestStarted(int)));
	}
}

void	WinMain::httpRequestDone(bool b)
{
	if (b == false)
	{
		QByteArray contenu = _http->readAll();

		QString str(contenu);
		QMessageBox::information(this, "test", str);

		_wl->setVisible(false);
		_wt = new WinTalk();
		_wt->show();
		//	this->setFixedSize(1600, 800);
		this->setCentralWidget(_wt);
	}
	else
	{
		_wl->hideLoading();	
	}
}

void	WinMain::httpRequestStarted(int i)
{
	_wl->showLoading();
}